{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Velocity Data Download Script for Validation\n",
    "#### Working Script to pull large velocity datasets and later compare to ICESAT2-derived velocities\n",
    "\n",
    "ICESat-2 hackweek  \n",
    "June 15, 2020  \n",
    "Lynn Kaluzienski"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import necessary modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import requests\n",
    "import zipfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "import pyproj\n",
    "import scipy, sys, os, pyproj, glob\n",
    "import matplotlib.pyplot as plt\n",
    "from shapely.geometry import Point, Polygon\n",
    "# run matplotlib in 'widget' mode\n",
    "%matplotlib widget\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting git+https://github.com/tsutterley/pointCollection.git@pip\n",
      "  Cloning https://github.com/tsutterley/pointCollection.git (to revision pip) to /tmp/pip-req-build-hm4zii0k\n",
      "  Running command git clone -q https://github.com/tsutterley/pointCollection.git /tmp/pip-req-build-hm4zii0k\n",
      "  Running command git checkout -b pip --track origin/pip\n",
      "  Switched to a new branch 'pip'\n",
      "  Branch 'pip' set up to track remote branch 'pip' from 'origin'.\n",
      "Requirement already satisfied (use --upgrade to upgrade): pointCollection==1.0.0.0 from git+https://github.com/tsutterley/pointCollection.git@pip in /home/jovyan/.local/lib/python3.7/site-packages\n",
      "Requirement already satisfied: numpy in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pointCollection==1.0.0.0) (1.18.4)\n",
      "Requirement already satisfied: scipy in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pointCollection==1.0.0.0) (1.4.1)\n",
      "Requirement already satisfied: pyproj in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pointCollection==1.0.0.0) (2.6.1.post1)\n",
      "Requirement already satisfied: h5py in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pointCollection==1.0.0.0) (2.10.0)\n",
      "Requirement already satisfied: netCDF4 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pointCollection==1.0.0.0) (1.5.3)\n",
      "Requirement already satisfied: matplotlib in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pointCollection==1.0.0.0) (3.2.1)\n",
      "Requirement already satisfied: gdal in /srv/conda/envs/notebook/lib/python3.7/site-packages (from pointCollection==1.0.0.0) (3.0.4)\n",
      "Requirement already satisfied: six in /srv/conda/envs/notebook/lib/python3.7/site-packages (from h5py->pointCollection==1.0.0.0) (1.15.0)\n",
      "Requirement already satisfied: cftime in /srv/conda/envs/notebook/lib/python3.7/site-packages (from netCDF4->pointCollection==1.0.0.0) (1.1.3)\n",
      "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from matplotlib->pointCollection==1.0.0.0) (2.4.7)\n",
      "Requirement already satisfied: python-dateutil>=2.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from matplotlib->pointCollection==1.0.0.0) (2.7.5)\n",
      "Requirement already satisfied: kiwisolver>=1.0.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from matplotlib->pointCollection==1.0.0.0) (1.2.0)\n",
      "Requirement already satisfied: cycler>=0.10 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from matplotlib->pointCollection==1.0.0.0) (0.10.0)\n",
      "Building wheels for collected packages: pointCollection\n",
      "  Building wheel for pointCollection (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for pointCollection: filename=pointCollection-1.0.0.0-py3-none-any.whl size=82220 sha256=d9f1e1a5eb0d00d665a441e5dfa128381c42caa5317cd9a8e24914d996c2dbe5\n",
      "  Stored in directory: /tmp/pip-ephem-wheel-cache-e6x_udu6/wheels/29/6e/f2/1435b756513140ff2fc1dc41847ebca181fd3285efc91b3d86\n",
      "Successfully built pointCollection\n"
     ]
    }
   ],
   "source": [
    "#! cd ..; [ -d pointCollection ] || git clone https://www.github.com/smithB/pointCollection.git\n",
    "sys.path.append(os.path.join(os.getcwd(), '..'))\n",
    "\n",
    "#!python3 -m pip install --user git+https://github.com/tsutterley/pointCollection.git@pip\n",
    "import pointCollection as pc\n",
    "#stuck here, not sure how to import point collection \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Magic function to enable interactive plotting (zoom/pan) in Jupyter notebook\n",
    "#If running locally, this would be `%matplotlib notebook`, but since we're using Juptyerlab, we use widget\n",
    "%matplotlib widget\n",
    "#%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-86 -81 -81 -86 -86]\n",
      "[-55 -55 -65 -65 -55]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5af5df9bee23478b872ee2c50d485467",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cmap': 'viridis', 'clim': [0, 1000], 'extent': array([-887878.65809562, -400567.81840096,  200333.90920048,\n",
      "        561654.87344315]), 'origin': 'lower'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Measures2 Velocity Map')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Set up directories and spatial extent\n",
    "data_root='/srv/shared/surface_velocity/'\n",
    "#spatial_extent = np.array([-102, -76, -98, -74.5])\n",
    "spatial_extent = np.array([-65, -86, -55, -81])\n",
    "lat=spatial_extent[[1, 3, 3, 1, 1]]\n",
    "lon=spatial_extent[[2, 2, 0, 0, 2]]\n",
    "print(lat)\n",
    "print(lon)\n",
    "# project the coordinates to Antarctic polar stereographic\n",
    "xy=np.array(pyproj.Proj(3031)(lon, lat))\n",
    "# get the bounds of the projected coordinates \n",
    "XR=[np.nanmin(xy[0,:]), np.nanmax(xy[0,:])]\n",
    "YR=[np.nanmin(xy[1,:]), np.nanmax(xy[1,:])]\n",
    "#Load Data\n",
    "Measures_vel=pc.grid.data().from_geotif(os.path.join(data_root,'FIS_Velocity','Measures2_FIS_Vel.tif'), bounds=[XR, YR])\n",
    "Measures_vx=pc.grid.data().from_geotif(os.path.join(data_root,'FIS_Velocity','Measures2_FIS_Vx.tif'), bounds=[XR, YR])\n",
    "Measures_vy=pc.grid.data().from_geotif(os.path.join(data_root,'FIS_Velocity','Measures2_FIS_Vy.tif'), bounds=[XR, YR])\n",
    "\n",
    "\n",
    "# show the velocity map:\n",
    "plt.figure()\n",
    "Measures_vel.show(cmap='viridis', clim=[0, 1000])\n",
    "plt.plot(xy[0,:], xy[1,:])\n",
    "plt.title('Measures2 Velocity Map')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-86 -81 -81 -86 -86]\n",
      "[-55 -55 -65 -65 -55]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "361c5d414c864aecb91c003aadbe4587",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cmap': 'viridis', 'clim': [-500, 500], 'extent': array([-887878.65809562, -400567.81840096,  200333.90920048,\n",
      "        561654.87344315]), 'origin': 'lower'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Measures2 Vx component')"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
